package telnetserver;

import java.io.*;

public class Sources {
    private Telnet telnet;
    private Object ret;
    private String[] buttons;
    
    public Sources(Telnet t, Object r) {
        telnet = t;
        ret = r;
        String[] temp = new File("src/telnetserver/").list();
        buttons = new String[temp.length + 1];
        buttons[0] = "BACK";
        for (int i = 0; i < temp.length; i++) {
            buttons[i + 1] = temp[i];
        }
    }
    
    public void addUser(User u) {
        u.setCanWrite(false);
        u.setCanPress(true);
        
        u.clear();
        u.prepare(1, 2, 2, "My sources, formating might fuck it up, but theyre here:");
        
        u.setButtons(buttons, null);
        u.fixButtonsXY(2, 4, 1);
        u.drawButtons();
    }
    
    public void read(User u, String s) {
        int btn = u.getButton()[1];
        if (btn == 0) telnet.changeMode(u, ret);
        else {
            FileSender f = new FileSender(telnet, u, "src/telnetserver/" + buttons[btn], this);
            telnet.changeMode(u, f);
            f.start();
            
        }
    }
}
